package yxy.game.pm2.active.module.subordinateclass;

import org.joda.time.DateTime;
import org.redisson.api.RMap;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.module.battle.BattleModule;
import yxy.game.pm2.module.battle.BattleRecord;

import java.util.HashMap;
import java.util.Set;

public class ChanllengeVideoModule extends AreaModule {

	// bean===========================================================================
	private int id;
	private HashMap<Integer, ChanllengeVideoRecord> bestround = new HashMap<Integer, ChanllengeVideoRecord>(); // 最少回合数 3个 key:round value:ChanllengeVideoRecord
	private ChanllengeVideoRecord bestpower; // 最小战力

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public HashMap<Integer, ChanllengeVideoRecord> getBestround() {
		return bestround;
	}

	public void setBestround(HashMap<Integer, ChanllengeVideoRecord> bestround) {
		this.bestround = bestround;
	}

	public ChanllengeVideoRecord getBestpower() {
		return bestpower;
	}

	public void setBestpower(ChanllengeVideoRecord bestpower) {
		this.bestpower = bestpower;
	}

	// func===========================================================================

	public void record(String playerCode, BattleModule module, int id) {
		try {
			if (tryLock()) {
				recordBestRound(playerCode, module, id);
				recordBestPower(playerCode, module, id);
			}
		} catch (Exception e) {
		} finally {
			unlock();
		}
	}

	private void recordBestRound(String playerCode, BattleModule module, int id) {
		int round = module.getRound();
		HashMap<Integer, ChanllengeVideoRecord> map = getBestround();

		if (map.size() < 3) {
			String recordcode = String.format("VideoId%sRound%s", id, module.getRound());
			BattleRecord battleRecord = module.saveRecord(recordcode);

			ChanllengeVideoRecord record = new ChanllengeVideoRecord();
			record.setDate(DateTime.now());
			record.setPlayerCode(playerCode);
			record.setRecordCode(battleRecord.getCode());
			record.setRounds(module.getRound());

			map.put(round, record);
			setBestround(map);
			set();
			return;
		} else {
			int newround = module.getRound();

			Set<Integer> rounds = map.keySet();
			int maxround = 0;
			for (int r : rounds) {
				maxround = Math.max(maxround, r);
			}

			if (newround < maxround) {
				String recordcode = String.format("VideoId%sRound%s", id, module.getRound());
				BattleRecord battleRecord = module.saveRecord(recordcode);

				ChanllengeVideoRecord record = new ChanllengeVideoRecord();
				record.setDate(DateTime.now());
				record.setPlayerCode(playerCode);
				record.setRecordCode(battleRecord.getCode());
				record.setRounds(module.getRound());

				map.remove(maxround);
				map.put(round, record);
				setBestround(map);
				set();
				return;
			}
		}

	}

	private void recordBestPower(String playerCode, BattleModule module, int id) {
		long power = module.getTeamA().getPower();
		ChanllengeVideoRecord record = getBestpower();

		if (record == null) {
			String recordcode = String.format("VideoId%sRound%s", id, module.getRound());
			BattleRecord battleRecord = module.saveRecord(recordcode, 10);

			record = new ChanllengeVideoRecord();
			record.setDate(DateTime.now());
			record.setPlayerCode(playerCode);
			record.setRecordCode(battleRecord.getCode());
			record.setRounds(module.getRound());
			record.setPower(power);
			setBestpower(record);
			set();
			return;
		} else {
			if (power < record.getPower()) {
				String recordcode = String.format("VideoId%sRound%s", id, module.getRound());
				BattleRecord battleRecord = module.saveRecord(recordcode, 10);

				record = new ChanllengeVideoRecord();
				record.setDate(DateTime.now());
				record.setPlayerCode(playerCode);
				record.setRecordCode(battleRecord.getCode());
				record.setRounds(module.getRound());
				record.setPower(power);
				setBestpower(record);
				set();
				return;
			}
		}
	}

	// redis==========================================================================

	static public String key(int areaId) {
		return key(areaId, "CHANLLENGEID");
	}

	static private RMap<Integer, ChanllengeVideoModule> touchMap(int areaId) throws RedisException {
		return RedisManager.client(redis()).getMap(key(areaId));
	}

	public void set() {
		try {
			RMap<Integer, ChanllengeVideoModule> map = touchMap(getAreaId());
			map.put(getId(), this);
		} catch (RedisException e) {
		}
	}

	static public ChanllengeVideoModule get(int areaId, int id) {
		try {
			RMap<Integer, ChanllengeVideoModule> map = touchMap(areaId);
			if (!map.containsKey(id)) {
				ChanllengeVideoModule module = create(areaId, id);
				map.put(id, module);
			}
			return map.get(id);
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return null;
	}

	private static ChanllengeVideoModule create(int areaId, int id) {
		ChanllengeVideoModule module = new ChanllengeVideoModule();
		module.setId(id);
		module.setAreaId(areaId);
		return module;
	}

	public static ChanllengeVideoModule get(String playerCode, int floor) {
		try {
			AreaBean area = area(playerCode);
			return ChanllengeVideoModule.get(area.getAreaId(), floor);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getAreaId()));
	}
}
